package com.kingdee.eas.custom.common;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.MethodUtils;

import com.kingdee.eas.hr.time.app.YearVacationBalanceControllerBean;

public class ReflectionUtils {

	public static Object callMethod(Class targetClass, Object instance,
			String m, Object[] params) {
		Object result = null;

		Method method = null;
		// cls.get
		Class[] paramTypes = null;
		try {
			if (params != null && params.length > 0) {
				paramTypes = new Class[params.length];
				for (int i = 0; i < params.length; i++) {
					paramTypes[i] = params[i].getClass();
				}
			}

			method = targetClass.getDeclaredMethod(m, paramTypes);
			method.setAccessible(true);
			result = method.invoke(instance, params);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public static Object getField(Class targetClass, Object instance, String p) {
		Object result = null;

		Class cls = instance.getClass();
		Field field = null;
		try {
			field = cls.getDeclaredField(p);
			field.setAccessible(true);
			result = field.get(instance);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public static void setField(Class targetClass, Object instance, String p,
			Object v) {
		Class cls = instance.getClass();
		Field field = null;
		try {
			field = cls.getDeclaredField(p);
			field.setAccessible(true);
			field.set(instance, v);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
